package com.mall.common.plugin;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * 插件初始化，在spring中配置插件bean，等spring初始化完成之后，将插件注册到
 *
 */
public class PluginLoader implements BeanPostProcessor {

	public Object postProcessAfterInitialization(Object bean, String arg1)
			throws BeansException {
		return bean;
	}

	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		if (bean instanceof AutoRegisterPlugin) {
			AutoRegisterPlugin plugin = (AutoRegisterPlugin) bean;

			if (plugin.getBundleList() != null) {
				plugin.register();
				List<IPluginBundle> pluginBundelList = plugin.getBundleList();
				for (IPluginBundle bundle : pluginBundelList) {
					bundle.registerPlugin(plugin);
				}
				PluginContext.registerPlugin(plugin);
			}
		}

		return bean;
	}

}
